package register

import (
	"api/config"
	"api/libs/errs"
	"encoding/json"
	"fmt"
	"gitee.com/dreamwood/ez"
	"io"
	"net/http"
	"net/url"
	"os"
)

type Model struct {
	AppId    string   `json:"appId" gorm:"size:255;" flag:""`
	Name     string   `json:"name" gorm:"size:255;" flag:""`
	CnName   string   `json:"cnName" gorm:"size:255;" flag:""`
	Icon     string   `json:"icon" gorm:"size:255;" flag:""`
	Index    string   `json:"index" gorm:"size:255;" flag:""`
	Entry    string   `json:"entry" gorm:"size:255;" flag:""`
	Login    string   `json:"login" gorm:"size:255;" flag:""`
	Sort     int      `json:"sort" gorm:"" flag:""`
	Children []*Model `json:"children" gorm:"foreignKey:parent_id;" flag:"O2M"`
}

const confFileModel = "./app_conf_models.json"
const confFileModelMd5 = "./app_conf_models.txt"

var models []*Model

func AddModel(model *Model) {
	if models == nil {
		models = make([]*Model, 0)
	}
	models = append(models, model)
}

func NewModel(key string, name string, sort int) *Model {
	return &Model{
		Name:   key,
		CnName: name,
		Sort:   sort,
	}
}

func (m *Model) SetAppId(v string) *Model  { m.AppId = v; return m }
func (m *Model) SetName(v string) *Model   { m.Name = v; return m }
func (m *Model) SetCnName(v string) *Model { m.CnName = v; return m }
func (m *Model) SetIcon(v string) *Model   { m.Icon = v; return m }
func (m *Model) SetIndex(v string) *Model  { m.Index = v; return m }
func (m *Model) SetEntry(v string) *Model  { m.Entry = v; return m }
func (m *Model) SetLogin(v string) *Model  { m.Login = v; return m }
func (m *Model) SetSort(v int) *Model      { m.Sort = v; return m }

func (m *Model) AddSon(son *Model) *Model {
	if m.Children == nil {
		m.Children = make([]*Model, 0)
	}
	m.Children = append(m.Children, son)
	return m
}
func (m *Model) AddSons(sons ...*Model) *Model {
	for _, son := range sons {
		m.AddSon(son)
	}
	return m
}

func RegModels() {
	// 页面权限注册接口
	conf := config.GetService()
	authHost := conf.Register.Host
	var regUrlPage = fmt.Sprintf("http://%s/auth/admin/Model/_reg?app=%s", authHost, conf.App.Id)
	if !config.GetCore().IsDev {
		//从文件获取配置
		models = make([]*Model, 0)
		content := ez.ReadFile(confFileModel)
		errs.Try(json.Unmarshal(content, &models))
		lastMd5 := ez.ReadFile(confFileModelMd5)
		nowMd5 := ez.Md5(string(content))
		if string(lastMd5) == nowMd5 {
			return
		} else {
			ez.CreateFile(confFileModelMd5, []byte(nowMd5))
		}
	}

	if models == nil {
		ez.Debug("页面为空")
		errs.Create("页面为空")
		return
	}
	ez.Debug(fmt.Sprintf("前端页面注册地址：%s", regUrlPage))
	for _, data := range models {
		content, e := json.Marshal(data)
		resp, e := http.PostForm(regUrlPage, url.Values{"content": []string{string(content)}})
		if ez.PrintError(e) {
			return
		}
		if resp != nil {
			rst, err := io.ReadAll(resp.Body)
			if ez.PrintError(err) {
				return
			}
			_, err = os.Stdout.Write(rst)
			ez.PrintError(err)
		}
	}

}

func CreateModelConfigFile() {
	if config.GetCore().IsDev {
		content, _ := json.MarshalIndent(models, "", "\t")
		ez.CreateFile(confFileModel, content)
	}
}
